package com.binvshe.binvshe.message;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.binvshe.binvshe.account.Account;
import com.binvshe.binvshe.account.AccountManager;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.message.dao.Message;
import com.binvshe.binvshe.message.dao.MessageHelper;
import com.pachong.android.framework.httprequest.IHttpRequest;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * FileName: MessageReceiver
 *
 * @CretedBy : zofnk
 * @CreateDate: 2016/9/19 20:44
 * @E-mail: zofnk@vip.qq.com
 * @RevisionsDate：
 * @ModifyNotes：
 */
public class MessageReceiver extends BroadcastReceiver {

    private Context mContext;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        Bundle bundle = intent.getExtras();
        String obj = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        Log.d("MessageReceiver", obj);
        String msg = obj;
        if (TextUtils.isEmpty(msg)) {
            Log.d("MessageReceiver", "Message content is null or size is 0!");
        } else {
            try {
                Log.d("kyluzoi", "get message");
                JSONObject jobj = new JSONObject(msg);
                Message message = new Message();
                message.setMsg_id(jobj.optString("msg_id"));
                message.setIcon(jobj.optString("icon"));
                message.setTitle(jobj.optString("title"));
                message.setContent(jobj.optString("content"));
                message.setDeletable(jobj.optInt("deletable"));
                message.setType(jobj.optInt("type"));
                message.setDate(jobj.optString("date"));
                message.setExtra(jobj.optString("extra"));
                message.setIsRead(false);
                MessageHelper.getInstance().insertNewMessage(message);
                EventBus.getDefault().post(new MessageEvent(MessageEvent.MSG_COMMON));
                if (message.getType() == MessageType.MSG_TYPE_CLUB) {
                    Log.d("kyluzoi", "setAccount");
                    setAccount();
                }
            } catch (Exception e) {
                Log.d("MessageReceiver", "Server send error message");
                e.printStackTrace();
            }
        }
    }

    private void setAccount() {
        IHttpRequest request = new MyHttpRequest(mContext);
        String requestUrl = String.format(UrlCenter.USER, AccountManager.getCurrentAccount().getUserId());
        request.get(requestUrl, null, new DataRequestListener<Account>(Account.class) {
            @Override
            public void onSuccess(Account account) {
                AccountManager.setCurrentAccount(account);
                Log.d("kyluzoi", "account:" + account);
                EventBus.getDefault().post(new MessageEvent.EventClub());
            }

            @Override
            public void onStart(Context context, String url) {
                enableProgress(false);
                super.onStart(context, url);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
            }
        });
    }

}
